package cn.wolfcode.wolf2w.web.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.uitl.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("strategyCatalog")
public class StrategyCatalogController {
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IDestinationService destinationService;
   
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo){
       Page<StrategyCatalog> page =strategyCatalogService.query(qo);
       model.addAttribute("page",page);
        //直接返回页面
        return "strategyCatalog/list";
    }
    
    /**
     * 通过区域id删除数据
     * @par
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        strategyCatalogService.delete(id);
        return JsonResult.success();
    }
   
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOUpdate(StrategyCatalog strategyCatalog){
        //添加或者修改都把目的地名字带上
        Destination destination = destinationService.get(strategyCatalog.getDestId());
        strategyCatalog.setDestName(destination.getName());
        //直接返回页面
        if (StringUtils.hasLength(strategyCatalog.getId())){
            //1先查询  2再替换  3修改
            //这个是特殊的类,
            strategyCatalogService.update(strategyCatalog);
        }else {
          strategyCatalogService.save(strategyCatalog);
        }
       return JsonResult.success();
    }
}
